package cn.tedu.egarticle.controller;

import cn.tedu.egarticle.base.response.JsonResult;
import cn.tedu.egarticle.base.response.StatusCode;
import cn.tedu.egarticle.mapper.UserMapper;
import cn.tedu.egarticle.pojo.dto.UserArticleParam;
import cn.tedu.egarticle.pojo.dto.UserLoginParam;
import cn.tedu.egarticle.pojo.entity.Article;
import cn.tedu.egarticle.pojo.vo.ArticleVO;
import cn.tedu.egarticle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "1.用户管理模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam,@ApiIgnore HttpSession session){
        log.debug("开始进行用户登录:"+userLoginParam);
        UserVO userVO = userMapper.selectlogin(userLoginParam);
        log.debug("userVO="+userVO);
        System.out.println(userVO);
        if(userVO!=null){
            //将用户信息存入到session会话当中
            session.setAttribute("user",userVO);
            return new JsonResult(StatusCode.OPERATION_SUCCESS,userVO);
        }else {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }
    @PostMapping("article")
    @ApiOperation("发表文章")
    @ApiOperationSupport(order = 20)
    public JsonResult addArticle(@Validated @RequestBody UserArticleParam userArticleParam, @ApiIgnore HttpSession session){
        log.debug("开始发表文章:"+userArticleParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Article article = new Article();
        BeanUtils.copyProperties(userArticleParam,article);
        article.setUserId(userVO.getId());
        int num = userMapper.addArticle(article);
        if(num>0){
            return JsonResult.ok(article);
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("selectByArticleId")
    @ApiOperation("查询文章")
    @ApiOperationSupport(order = 30)
    public JsonResult selectByArticle(@Range(min = 1,max = 100,message = "id必须在1~100之间") @RequestParam Long id) {
        log.debug("开始查询文章:" + id);

        ArticleVO articleVO = userMapper.selectById(id);
        if(articleVO == null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok(articleVO);
    }
}
